﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

using PlayerScannerCore.ViewModel;

namespace PlayerScanner
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ViewModel _viewModel;
        private DispatcherTimer _timer;
        private FloorItemsWindow _floorItemsWindow;
        private SettingsWindow _settingsWindow;

        public MainWindow()
        {
            InitializeComponent();

            //Expose application settings to the model
            PlayerScannerCore.Properties.Settings.Default = Properties.Settings.Default;

            DataContext = _viewModel = new ViewModel();
            _floorItemsWindow = new FloorItemsWindow();
            _floorItemsWindow.ViewModel = _viewModel.FloorViewModel;
            _settingsWindow = new SettingsWindow();
            _timer = new DispatcherTimer() { Interval = new TimeSpan(0, 0, 1) };
            _timer.Tick += Timer_Tick;
            _timer.Start();
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            //The whole UI is refreshed every timeout by refreshing each view model in the heirarchy
            //starting with this root view model.
            _viewModel.Refresh();
        }

        private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            UpdateViewModelSelectedTab();
        }

        private void UpdateViewModelSelectedTab()
        {
            ////This is a bit of an ugly way of doing this. It should really be handled through binding
            ////but in the interest of time it was left this way.

            //var selectedTab = tabs.SelectedItem;
            //TabIndices selectedTabIndex = TabIndices.Inventory;

            //if (selectedTab == tabInventory)
            //    selectedTabIndex = TabIndices.Inventory;
            //else if (selectedTab == tabTechsTraps)
            //    selectedTabIndex = TabIndices.Techs;
            //else if (selectedTab == tabStats)
            //    selectedTabIndex = TabIndices.Stats;

            //_viewModel.PlayerViewModel.SelectedTab = selectedTabIndex;
        }

        private void SettingsMenuItemClick(object sender, RoutedEventArgs e)
        {
            _settingsWindow.Show();
            _settingsWindow.Activate();
        }

        private void ExitMenuItemClick(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void FloorItemsMenuItemClick(object sender, RoutedEventArgs e)
        {
            _floorItemsWindow.Show();
            _floorItemsWindow.Activate();
        }
    }
}
